<?php
define('DB_HOST', 'localhost');
define('DB_PASS', 'asd22d');
define('DB_USER', 'root');
define('DB_NAME', 'tattoop2');

function db_connect() {
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    $db_selected = mysql_select_db(DB_NAME, $link);
    if (!$db_selected) {
        die('Can\'t select database: ' . mysql_error());
    }
}

$link = db_connect();

function setLastActivity($username) {
    //global $link;
	$query = "update autori set poslednji_pristup=CURRENT_TIMESTAMP() where username='$username' limit 1;";
    mysql_query($query) or die(mysql_error());
}

function getUserInfo($username) {
    $user = array();

    //global $link;
	$query = "select a.id,
a.username,password,a.Ime,datrodjenja,a.mesto,a.drzava,www,prikazivanje_maila,
email,nivopristupa,
replace(a.datum_registracije,' ','.') as datum_registracije,
pitanje,odgovor,fotografiseod,
interesovanja,osebi,a.slika,avatar,
poslednji_pristup,
status,a.rejting,mailnot,adresa_studija,
premium,f.username,f.ime,
broj_fotografija,broj_pregleda,broj_skljoceva,broj_komentara, telefon_privatni, telefon_studio, email_studio, www_studio
from autori as a
inner join pregled_fotografa as f
on a.username=f.username
and a.username='$username' limit 1;";

    $result = mysql_query($query) or die(mysql_error());
    $user = mysql_fetch_assoc($result);
    return $user;
}

function totalClanovi() {
    //global $link;
	$query = "select count(*) from autori";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result);
    return $row[0];
}

function getClanoviList($limit) {
    $clanovi = array();

    //global $link;
	$query = "SELECT * FROM pregled_fotografa order by username $limit;";
    $result = mysql_query($query) or die(mysql_error());

    while ($row = mysql_fetch_array($result)) {
        array_push($clanovi, $row);
    }

    return $clanovi;
}

function getShoutboxList() {
    $shoutbox = array();

    //global $link;
	$query = "select * from (select w.messageid, w.name, w.url, w.message, w.ip, w.date, a.id, a.username
    from wtagshoutbox w, autori a where name=username ORDER BY messageid DESC LIMIT 15) as shoutbox
    ORDER BY messageid ASC;";
    $result = mysql_query($query) or die(mysql_error());

    while ($row = mysql_fetch_array($result)) {
        $row['message'] = convertToTinyUrl($row['message']);
        array_push($shoutbox, $row);
    }

    return $shoutbox;
}

function getTextPosition($text, $id) {
    //global $link;
	$query = "select LOCATE('$text', tekst) from `blog_poruke1` where id=$id;";
    $result = mysql_query($query) or die(mysql_error());

    $row = mysql_fetch_array($result);

    return $row[0];
}

function getFirstImage($id) {
    $start = getTextPosition('src=', $id) + 5;
    $end = getTextPosition('.jpg', $id) + 4;

    $length = $end - $start;

    //global $link;
	$query = "select substring(tekst, $start, $length) from blog_poruke1 where id=$id;";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result);

    return $row[0];
}

function getPostExcerpt($number) {
    //global $link;
	$query = "select * from (SELECT * FROM blog_poruke1 order by id desc limit $number) as poruke order by id asc;";
    $result = mysql_query($query) or die(mysql_error());
    $post = array();

    while ($row = mysql_fetch_array($result)) {
        $row['slika'] = getFirstImage($row['id']);
        $temptext = strip_tags($row['tekst']);
        $row['tekst'] = substr($temptext, 0, 200);
        array_push($post, $row);
    }
    return $post;
}

function getPostTitles($limit, $offset) {
    //global $link;
	$query = "select * from (SELECT * FROM blog_poruke1 order by id desc limit $limit, $offset) as poruke order by id asc;";
    $result = mysql_query($query) or die(mysql_error());
    $otherPost = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($otherPost, $row);
    }

    return $otherPost;
}

//TO DO: return $row[0] ne treba while, promeniti u pocetna.php nakon toga i u magazin_admin.php
function getSinglePost($id) {
    //global $link;
	$query = "select * from blog_poruke1 where id = $id;";
    $result = mysql_query($query) or die(mysql_error());
    $singlePost = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($singlePost, $row);
    }

    return $singlePost;
}

function totalMainPostsByCategory($category) {
    //global $link;
	$query = "select count(*) from blog_poruke1 where kategorija='$category';";
    $result = mysql_query($query) or die(mysql_error());
    $totalMainPosts = mysql_fetch_array($result);

    return $totalMainPosts[0];
}

function totalMainPosts() {
    //global $link;
	$query = "select count(*) from blog_poruke1;";
    $result = mysql_query($query) or die(mysql_error());
    $totalMainPosts = mysql_fetch_array($result);

    return $totalMainPosts[0];
}

function getMainPostByCategory($limit, $category) {
    //global $link;
	$query = "select * from blog_poruke1 where kategorija='$category' order by id $limit;";
    $result = mysql_query($query) or die(mysql_error());
    $mainPost = array();

    while ($row = mysql_fetch_array($result)) {
        $row['slika'] = getFirstImage($row['id']);
        $temptext = strip_tags($row['tekst']);
        $row['tekst'] = substr($temptext, 0, 300);

        array_push($mainPost, $row);
    }

    return $mainPost;
}

function getMainPost($limit) {
    //global $link;
	$query = "select * from blog_poruke1 order by id $limit;";
    $result = mysql_query($query) or die(mysql_error());
    $mainPost = array();

    while ($row = mysql_fetch_array($result)) {
        $row['slika'] = getFirstImage($row['id']);
        $temptext = strip_tags($row['tekst']);
        $row['tekst'] = substr($temptext, 0, 300);

        array_push($mainPost, $row);
    }

    return $mainPost;
}

function insertChatMessage($message, $username) {
    //global $link;
	$query = "insert into wtagshoutbox (name, message, date) values ('$username', '$message', CURRENT_TIMESTAMP())";
    mysql_query($query) or die(mysql_error());
}

function insertPoolVotes($id_ankete, $username, $pool_select) {
    //global $link;
	$query = "insert into anketaglasovi (id_ankete, username, datum, glas) values('$id_ankete', '$username', CURRENT_TIMESTAMP(),$pool_select)";
    mysql_query($query) or die(mysql_error());
}

function countPoolFirstAnswers() {
    //global $link;
	$query = "select count(*) from anketaglasovi where glas=1;";
    $result = mysql_query($query) or die(mysql_error());
    $poolFirstAnswers = mysql_fetch_array($result);

    return $poolFirstAnswers[0];
}

function countPoolSecoundAnswers() {
    //global $link;
	$query = "select count(*) from anketaglasovi where glas=2;";
    $result = mysql_query($query) or die(mysql_error());
    $poolSecoundAnswers = mysql_fetch_array($result);

    return $poolSecoundAnswers[0];
}

function countPoolThirdAnswers() {
    //global $link;
	$query = "select count(*) from anketaglasovi where glas=3;";
    $result = mysql_query($query) or die(mysql_error());
    $poolThirdAnswers = mysql_fetch_array($result);

    return $poolThirdAnswers[0];
}

function countPoolFourthAnswers() {
    //global $link;
	$query = "select count(*) from anketaglasovi where glas=4;";
    $result = mysql_query($query) or die(mysql_error());
    $poolFourthAnswers = mysql_fetch_array($result);

    return $poolFourthAnswers[0];
}

function getActiveUsers() {
    //global $link;
	$query = "select count(*) from `autori` where poslednji_pristup  between (select CURRENT_TIMESTAMP() - INTERVAL 15 minute) and (select CURRENT_TIMESTAMP());";
    $result = mysql_query($query) or die(mysql_error());
    $activeUsers = mysql_fetch_array($result);

    return $activeUsers[0];
}

function getLoggedUsers() {
    //global $link;
	$query = "select * from autori where poslednji_pristup between (select CURRENT_TIMESTAMP() - INTERVAL 15 minute) and (select CURRENT_TIMESTAMP());";
    $result = mysql_query($query) or die(mysql_error());
    $activeUsers = array();
    while ($row = mysql_fetch_array($result)) {
        array_push($activeUsers, $row);
    }
    return $activeUsers;
}

function insertCommentMessage($id_bloga, $komentar, $username) {
    //global $link;
	$query = "insert into blog_komentari1 (id_bloga, datum_vreme_upisa, komentar, username, pregledano ) values ('$id_bloga',  CURRENT_TIMESTAMP(), '$komentar', '$username', 1);";
    mysql_query($query) or die(mysql_error());
}

function getNumberOfComments($id_bloga) {
    //global $link;
	$query = "select count(*) from blog_komentari1 where id_bloga=$id_bloga;";
    $result = mysql_query($query) or die(mysql_error());
    $numberOfComments = mysql_fetch_array($result);

    return $numberOfComments[0];
}

function getCommentMessages($id_bloga) {
    //global $link;
	$query = "select b.id, b.id_bloga, b.datum_vreme_upisa, b.komentar, b.username, a.ime, a.slika, a.id as clan_id
    FROM blog_komentari1 b, autori a WHERE b.username = a.username and b.id_bloga=$id_bloga;";
    $result = mysql_query($query) or die(mysql_error());
    $commentMessages = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($commentMessages, $row);
    }

    return $commentMessages;
}

function getClanInfo($id) {
    $clanInfo = array();

    //global $link;
	$query = "select a.id,
a.username,password,a.Ime,datrodjenja,a.mesto,a.drzava,www,prikazivanje_maila,prikazivanje_ptel,telefon_privatni,
email,nivopristupa,adresa_studija, telefon_studio, email_studio, www_studio,
replace(a.datum_registracije,' ','.') as datum_registracije,
pitanje,odgovor,fotografiseod,
interesovanja,osebi,a.slika,avatar,
poslednji_pristup,
status,a.rejting,mailnot,
premium,f.username,f.ime,
broj_fotografija,broj_pregleda,broj_skljoceva,broj_komentara
from autori as a
inner join pregled_fotografa as f
on a.username=f.username
and a.id='$id';";
    $result = mysql_query($query) or die(mysql_error());

    while ($row = mysql_fetch_array($result)) {
        array_push($clanInfo, $row);
    }

    return $clanInfo;
}

function getLastPhotos() {
    //global $link;
	$query = "select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja, f.vreme_fotografisanja,
f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso, f.dozvoljeno_komentarisanje, f.dozvoljeno_ocenjivanje,
f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata, f.autorovkomentar, f.broj_favorita,
f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava, a.id as ime from fotografija f, autori a
where a.username = f.username ORDER BY f.id desc limit 18;";
    $result = mysql_query($query) or die(mysql_error());
    $lastPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($lastPhotos, $row);
    }

    return $lastPhotos;
}

function getSinglePhoto($single_photo_id) {
    //global $link;
	$query = "SELECT * FROM `fotografija` where id=$single_photo_id;";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_assoc($result);
    return $row;
}

function getOtherPhotos($username) {
    //global $link;
	$query = "SELECT * FROM `fotografija` where username='$username';";
    $result = mysql_query($query) or die(mysql_error());
    $otherPhotos = array();
    while ($row = mysql_fetch_array($result)) {
        array_push($otherPhotos, $row);
    }
    return $otherPhotos;
}

function getPhotoComments($photo_id) {
    //global $link;
	$query = "SELECT k.*,a.slika, a.id as autor_id
              FROM komentari k
              INNER JOIN autori a ON k.username=a.username
              AND id_fotografije=$photo_id
              ORDER BY id;";
    $result = mysql_query($query) or die(mysql_error());
    $photoComments = array();
    while ($row = mysql_fetch_assoc($result)) {
        array_push($photoComments, $row);
    }
    return $photoComments;
}

function incrementImagelikes($photo_id) {
    //global $link;
	$query = "update fotografija set broj_skljoceva=broj_skljoceva+1 where id=$photo_id";
    mysql_query($query) or die(mysql_error());
    return true;
}

function incrementImageViews($photo_id) {
    //global $link;
	$query = "update fotografija set broj_pregleda=broj_pregleda+1 where id=$photo_id";
    mysql_query($query) or die(mysql_error());
}

function addImageComment($photo_id, $comment, $username) {
    //global $link;
	$query = "insert into komentari (datum_vreme_upisa, id_fotografije, komentar, pregledano, username)
                values (convert(CURRENT_TIMESTAMP(), char(20)), $photo_id, '$comment', 1, '$username')";
    mysql_query($query) or die(mysql_error());
}

function getImageInfo($photo_id) {
    //global $link;
	$query = "SELECT
            id, username, naziv, opis, kljucne_reci, datum_postavljanja,
            slika, broj_skljoceva, broj_pregleda, galerija, autorovkomentar
            FROM fotografija
            WHERE ID=$photo_id;";
    $result = mysql_query($query) or die(mysql_error());
    $image = mysql_fetch_assoc($result);
    return $image;
}

function updateNalogPersonal($username, $ime, $datum_rodjenja, $grad, $drzava, $telefon_privatni, $web, $email) {
    //global $link;
	$query = "update autori set Ime='$ime', datrodjenja='$datum_rodjenja', mesto='$grad', drzava='$drzava', telefon_privatni='$telefon_privatni', www='$web', email='$email' where username='$username'";
    mysql_query($query) or die(mysql_error());
}

function updateNalogStudio($username, $odgovor, $adresa_studija, $telefon_studio, $email_studio, $web_studio) {
    //global $link;
	$query = "update autori set odgovor='$odgovor', adresa_studija='$adresa_studija', telefon_studio='$telefon_studio',
    email_studio='$email_studio', www_studio='$web_studio' where username='$username'";
    mysql_query($query) or die(mysql_error());
}

function updateNalogOsebi($username, $osebi) {
    //global $link;
	$query = "update autori set osebi='$osebi' where username='$username'";
    mysql_query($query) or die(mysql_error());
}

function getLastPhotos2() {

    //global $link;
	$query = "select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja, f.vreme_fotografisanja,
f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso, f.dozvoljeno_komentarisanje, f.dozvoljeno_ocenjivanje,
f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata, f.autorovkomentar, f.broj_favorita,
f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava, a.id as ime from fotografija f, autori a
where a.username = f.username ORDER BY f.id desc limit 18,18;";
    $result = mysql_query($query) or die(mysql_error());
    $lastPhotos2 = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($lastPhotos2, $row);
    }

    return $lastPhotos2;
}

function getMostCommentedPhotos() {

    //global $link;
	$query = "SELECT max(k.id) as kid,f.id, f.slika, f.username, f.naziv, a.id as ime, f.broj_komentara
FROM komentari k
inner join fotografija f on f.id=k.id_fotografije
inner join autori a on f.username=a.username
group by f.id, f.slika, f.username, f.naziv, a.id, f.broj_komentara
order by 1 desc limit 18;";
    $result = mysql_query($query) or die(mysql_error());
    $MostCommentedPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($MostCommentedPhotos, $row);
    }

    return $MostCommentedPhotos;
}

function getMostCommentedPhotos2() {

    //global $link;
	$query = "SELECT max(k.id) as kid,f.id, f.slika, f.username, f.naziv, a.id as ime, f.broj_komentara
FROM komentari k
inner join fotografija f on f.id=k.id_fotografije
inner join autori a on f.username=a.username
group by f.id, f.slika, f.username, f.naziv, a.id, f.broj_komentara
order by 1 desc limit 18,18;";
    $result = mysql_query($query) or die(mysql_error());
    $MostCommentedPhotos2 = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($MostCommentedPhotos2, $row);
    }

    return $MostCommentedPhotos2;
}

function getMostPopularPhotos() {

    //global $link;
	$query = "select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja, f.vreme_fotografisanja,
f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso, f.dozvoljeno_komentarisanje, f.dozvoljeno_ocenjivanje,
f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata, f.autorovkomentar, f.broj_favorita,
f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava, a.id as ime from fotografija f, autori a
where a.username = f.username ORDER BY broj_pregleda desc limit 18;";
    $result = mysql_query($query) or die(mysql_error());
    $MostPopularPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($MostPopularPhotos, $row);
    }

    return $MostPopularPhotos;
}

function getMostPopularPhotos2() {

    //global $link;
	$query = "select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja, f.vreme_fotografisanja,
f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso, f.dozvoljeno_komentarisanje, f.dozvoljeno_ocenjivanje,
f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata, f.autorovkomentar, f.broj_favorita,
f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava, a.id as ime from fotografija f, autori a
where a.username = f.username ORDER BY broj_pregleda desc limit 18,18;";
    $result = mysql_query($query) or die(mysql_error());
    $MostPopularPhotos2 = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($MostPopularPhotos2, $row);
    }

    return $MostPopularPhotos2;
}

function getTotalGallery($tip_galerije) {

    //global $link;
	$query = "select count(*) from fotografija where galerija='$tip_galerije';";
    $result = mysql_query($query) or die(mysql_error());
    $totalGallery = mysql_fetch_array($result);
    return $totalGallery[0];
}

function getGalleryPhotos($tip_galerije, $limit) {

    //global $link;
	$query = "select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja,
f.vreme_fotografisanja,
f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso, f.dozvoljeno_komentarisanje, f.dozvoljeno_ocenjivanje,
f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata, f.autorovkomentar, f.broj_favorita,
f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava, a.id as ime from fotografija f, autori a
where a.username = f.username and galerija='$tip_galerije' order by id $limit;";
    $result = mysql_query($query) or die(mysql_error());
    $GalleryPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($GalleryPhotos, $row);
    }

    return $GalleryPhotos;
}

function getTotalUserColourGallery($user) {

    //global $link;
	$query = "select count(*) from fotografija where username = '$user' and galerija='Colour';";
    $result = mysql_query($query) or die(mysql_error());
    $totalUserColourGallery = mysql_fetch_array($result);
    return $totalUserColourGallery[0];
}

function getUserColourGalleryPhotos($user, $limit) {

    //global $link;
	$query = "select * from fotografija where username='$user' and galerija='Colour' $limit;";

    $result = mysql_query($query) or die(mysql_error());
    $userColourGalleryPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($userColourGalleryPhotos, $row);
    }

    return $userColourGalleryPhotos;
}

function getTotalUserBlackandGrayGallery($user) {

    //global $link;
	$query = "select count(*) from fotografija where username = '$user' and galerija='Black and Gray';";
    $result = mysql_query($query) or die(mysql_error());
    $totalUserBlackandGrayGallery = mysql_fetch_array($result);
    return $totalUserBlackandGrayGallery[0];
}

function getUserBlackandGrayGalleryPhotos($user, $limit) {

    //global $link;
	$query = "select * from fotografija where username='$user' and galerija='Black and Gray' $limit;";

    $result = mysql_query($query) or die(mysql_error());
    $userBlackandGrayGalleryPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($userBlackandGrayGalleryPhotos, $row);
    }

    return $userBlackandGrayGalleryPhotos;
}

function getTotalUserCoverUpGallery($user) {

    //global $link;
	$query = "select count(*) from fotografija where username = '$user' and galerija='Cover Up';";
    $result = mysql_query($query) or die(mysql_error());
    $totalUserCoverUpGallery = mysql_fetch_array($result);
    return $totalUserCoverUpGallery[0];
}

function getUserCoverUpGalleryPhotos($user, $limit) {

    //global $link;
	$query = "select * from fotografija where username='$user' and galerija='Cover Up' $limit;";

    $result = mysql_query($query) or die(mysql_error());
    $userCoverUpGalleryPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($userCoverUpGalleryPhotos, $row);
    }

    return $userCoverUpGalleryPhotos;
}

function getTotalUserPiercingGallery($user) {

    //global $link;
	$query = "select count(*) from fotografija where username = '$user' and galerija='Piercing';";
    $result = mysql_query($query) or die(mysql_error());
    $totalUserPiercingGallery = mysql_fetch_array($result);
    return $totalUserPiercingGallery[0];
}

function getUserPiercingGalleryPhotos($user, $limit) {

    //global $link;
	$query = "select * from fotografija where username='$user' and galerija='Piercing' $limit;";

    $result = mysql_query($query) or die(mysql_error());
    $userPiercingGalleryPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($userPiercingGalleryPhotos, $row);
    }

    return $userPiercingGalleryPhotos;
}

function getTotalUserFlashGallery($user) {

    //global $link;
	$query = "select count(*) from fotografija where username = '$user' and galerija='Flash';";
    $result = mysql_query($query) or die(mysql_error());
    $totalUserFlashGallery = mysql_fetch_array($result);
    return $totalUserFlashGallery[0];
}

function getUserFlashGalleryPhotos($user, $limit) {

    //global $link;
	$query = "select * from fotografija where username='$user' and galerija='Flash' $limit;";

    $result = mysql_query($query) or die(mysql_error());
    $userFlashGalleryPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($userFlashGalleryPhotos, $row);
    }

    return $userFlashGalleryPhotos;
}

function getTotalUserPersonalGallery($user) {

    //global $link;
	$query = "select count(*) from fotografija where username = '$user' and galerija='Personal';";
    $result = mysql_query($query) or die(mysql_error());
    $totalUserPersonalGallery = mysql_fetch_array($result);
    return $totalUserPersonalGallery[0];
}

function getUserPersonalGalleryPhotos($user, $limit) {

    //global $link;
	$query = "select * from fotografija where username='$user' and galerija='Personal' $limit;";

    $result = mysql_query($query) or die(mysql_error());
    $userPersonalGalleryPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($userPersonalGalleryPhotos, $row);
    }

    return $userPersonalGalleryPhotos;
}

function getTotalPublishedPhotos($clan_id) {
    //global $link;
	$query = "select count(*) from (select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja,
	f.vreme_fotografisanja,
	f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso, f.dozvoljeno_komentarisanje,
	f.dozvoljeno_ocenjivanje,
	f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata, f.autorovkomentar, f.broj_favorita,
	f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava, a.id as ime from fotografija f, autori a
	where a.username = f.username and a.id='$clan_id')as Photos;";

    $result = mysql_query($query) or die(mysql_error());
    $totalPublishedPhotos = mysql_fetch_array($result);
    return $totalPublishedPhotos[0];
}

function getTotalCommentedPhotos($clan_id) {
    //global $link;
	$query = "select count(*) from (SELECT max(k.id) as kid,f.id, f.slika, f.username, f.naziv, a.id as ime, f.broj_komentara
	FROM komentari k
	inner join fotografija f on f.id=k.id_fotografije
	inner join autori a on f.username=a.username
	group by f.id, f.slika, f.username, f.naziv, a.id, f.broj_komentara
	order by 1 desc) as poslednje_komentarisani where ime=$clan_id ;";

    $result = mysql_query($query) or die(mysql_error());
    $totalCommentedPhotos = mysql_fetch_array($result);
    return $totalCommentedPhotos[0];
}

function getPublishedPhotos($clan_id, $limit) {
    //global $link;
	$query = "select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja,
	f.vreme_fotografisanja,
	f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso, f.dozvoljeno_komentarisanje,
	f.dozvoljeno_ocenjivanje,
	f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata, f.autorovkomentar, f.broj_favorita,
	f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava, a.id as ime from fotografija f, autori a
	where a.username = f.username and a.id='$clan_id' $limit;";

    $result = mysql_query($query) or die(mysql_error());
    $PublishedPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($PublishedPhotos, $row);
    }

    return $PublishedPhotos;
}

function getPopularPhotos($clan_id, $limit) {
    //global $link;
	$query = "select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja,
	f.vreme_fotografisanja,
	f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso, f.dozvoljeno_komentarisanje,
	f.dozvoljeno_ocenjivanje,
	f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata, f.autorovkomentar, f.broj_favorita,
	f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava, a.id as ime from fotografija f, autori a
	where a.username = f.username and a.id=$clan_id order by f.broj_pregleda desc $limit;";

    $result = mysql_query($query) or die(mysql_error());
    $PopularPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($PopularPhotos, $row);
    }

    return $PopularPhotos;
}

function getLastCommentedPhotos($clan_id, $limit) {
    //global $link;
	$query = "select * from (SELECT max(k.id) as kid,f.id, f.slika, f.username, f.naziv, a.id as ime, f.broj_komentara
	FROM komentari k
	inner join fotografija f on f.id=k.id_fotografije
	inner join autori a on f.username=a.username
	group by f.id, f.slika, f.username, f.naziv, a.id, f.broj_komentara
	order by 1 desc) as poslednje_komentarisani where ime=$clan_id $limit ;";

    $result = mysql_query($query) or die(mysql_error());
    $LastCommentedPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($LastCommentedPhotos, $row);
    }

    return $LastCommentedPhotos;
}

//Magazin admin
function insertNewMagazinClanak($naslov, $clanak, $username, $category) {
    //global $link;
	$query = "insert into blog_poruke1 (datum, username, naslov, tekst, kategorija)
                values (CURRENT_TIMESTAMP(), '$username', '$naslov', '$clanak', '$category')";
    mysql_query($query) or die(mysql_error());
    return mysql_insert_id();
}

function updateMagazinClanak($post_id, $naslov, $clanak, $category, $username) {
    //global $link;
	$query = "update blog_poruke1 set datum=CURRENT_TIMESTAMP(), username='$username', naslov='$naslov',
                tekst='$clanak', kategorija='$category' where id=$post_id";
    mysql_query($query) or die(mysql_error());
}

function getCategories() {
    //global $link;
	$query = "select distinct kategorija from blog_poruke1 where kategorija<>'Nekategorizovano';";
    $result = mysql_query($query) or die(mysql_error());
    $categories = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($categories, $row);
    }

    return $categories;
}

function getVideoCategories() {
    //global $link;
	$query = "select distinct kategorija from video;";
    $result = mysql_query($query) or die(mysql_error());
    $categories = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($categories, $row);
    }

    return $categories;
}

function getAllPostTitles() {
    //global $link;
	$query = "SELECT id, naslov FROM blog_poruke1 order by id desc;";
    $result = mysql_query($query) or die(mysql_error());
    $allPosts = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($allPosts, $row);
    }

    return $allPosts;
}

function getLastVideo() {
    //global $link;
	$query = "SELECT url FROM `video` order by id desc limit 1;";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result);
    return $row[0];
}

function getLastVideoByCategory($kategorija) {
    //global $link;
	$query = "SELECT url FROM `video` where kategorija='$kategorija' order by id desc limit 1;";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result);
    return $row[0];
}

function getMainVideoTags($mainvideo) {
    //global $link;
	$query = "select tag from video where url='$mainvideo'";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result);
    return $row[0];
}

function getMainVideoDescription($mainvideo) {
    //global $link;
	$query = "select description from video where url='$mainvideo'";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result);
    return $row[0];
}

function getSidebarVideos($notshown, $limit) {
    //global $link;
	$query = "SELECT url FROM `video` where url != '$notshown' $limit ;";
    $result = mysql_query($query) or die(mysql_error());

    $SidebarVideos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($SidebarVideos, $row);
    }

    return $SidebarVideos;
}

function getSidebarVideosByCategory($notshown, $limit, $kategorija) {
    //global $link;
	$query = "SELECT url FROM `video` where url != '$notshown' and kategorija='$kategorija' $limit;";
    $result = mysql_query($query) or die(mysql_error());

    $SidebarVideos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($SidebarVideos, $row);
    }

    return $SidebarVideos;
}

function getTotalSidebarVideos($notshown) {

    //global $link;
	$query = "select count(*) url from video where url !='$notshown';";
    $result = mysql_query($query) or die(mysql_error());
    $totalSidebarVideos = mysql_fetch_array($result);
    return $totalSidebarVideos[0];
}

function getTotalSidebarVideosByCategory($notshown, $kategorija) {

    //global $link;
	$query = "select count(*) url from video where url !='$notshown' and kategorija='$kategorija';";
    $result = mysql_query($query) or die(mysql_error());
    $totalSidebarVideos = mysql_fetch_array($result);
    return $totalSidebarVideos[0];
}

function checkIfUsernameExist($username) {
    //global $link;
	$query = "select * from autori where username = '$username';";
    $result = mysql_query($query) or die(mysql_error());

    $count = mysql_num_rows($result);

    if ($count < 1) {
        return false;
    } else {
        return true;
    }
}

function checkIfEmailExist($email) {
    //global $link;
	$query = "select * from autori where email = '$email';";
    $result = mysql_query($query) or die(mysql_error());

    $count = mysql_num_rows($result);

    if ($count < 1) {
        return false;
    } else {
        return true;
    }
}

function portalDbRegister($user) {
    $today = date("d m Y");
    //global $link;
	$query = "insert into autori (username, Ime, password, datrodjenja, mesto, drzava, www,
email,  nivopristupa, datum_registracije, telefon_privatni,
odgovor, adresa_studija, telefon_studio, email_studio,
www_studio, osebi,
prikazivanje_maila, prikazivanje_ptel) values
(
'" . $user['username'] . "',
'" . $user['ime'] . "',
'" . $user['password'] . "',
'" . $user['datum_rodj'] . "',
'" . $user['grad'] . "',
'" . $user['drzava'] . "',
'" . $user['www'] . "',
'" . $user['email'] . "',
    1, '$today' ,
'" . $user['telefon'] . "',
'" . $user['studio'] . "',
'" . $user['adresa_studija'] . "',
'" . $user['telefon_studio'] . "',
'" . $user['email_studio'] . "',
'" . $user['www_studio'] . "',
'" . $user['osebi'] . "',
'" . $user['prikaz_email'] . "',
'" . $user['prikaz_telefon'] . "'
);";
    return mysql_query($query) or die(mysql_error());
}

function updateProfilPicture($slika, $username) {
    //global $link;
	$query = "update autori set slika='$slika' where username='$username' limit 1;";
    mysql_query($query) or die(mysql_error());
}

function updateGalleryPicture($naziv, $opis, $kljucne_reci, $username, $slika, $galerija) {
    //global $link;
	$query = "insert into fotografija(username, naziv, opis, kljucne_reci, datum_postavljanja, slika, galerija)
        values('$username', '$naziv', '$opis', '$kljucne_reci', CURDATE(), '$slika', '$galerija')";
    mysql_query($query) or die(mysql_error());
}

function insertVideoComment($id_video, $komentar, $username) {
    //global $link;
	$query = "insert into video_komentari (id_video, username, komentar, datum_vreme_upisa) values ('$id_video','$username', '$komentar', CURRENT_TIMESTAMP());";
    mysql_query($query) or die(mysql_error());
}

function getNumberOfVideoComments($id_video) {
    //global $link;
	$query = "select count(*) from video_komentari where id_video='$id_video';";
    $result = mysql_query($query) or die(mysql_error());
    $numberOfVideoComments = mysql_fetch_array($result);

    return $numberOfVideoComments[0];
}

function getVideoComments($url) {
    //global $link;
	$query = "select v.url, v.username as admin_upload, k.id as id_komentara, k.id_video, k.username, k.komentar,
    k.datum_vreme_upisa, a.username as autori_username, a.slika, a.id as autor_id from video v, video_komentari k, autori a
    where v.url = id_video and k.username = a.username and url='$url';";
    $result = mysql_query($query) or die(mysql_error());
    $commentVideo = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($commentVideo, $row);
    }

    return $commentVideo;
}

function insertVideo($YoutubeCode, $username, $description, $tag, $kategorija, $naslov) {
    //global $link;
	$query = "insert into video (url, username, description, tag, kategorija, naslov) values ('$YoutubeCode', '$username', '$description', '$tag', '$kategorija', '$naslov')";
    mysql_query($query) or die(mysql_error());
}

function searchPhotos($kljucne, $opis, $naziv, $sortiranje, $limit) {
    //global $link;
	$query = "select * from (select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja,
    f.vreme_fotografisanja,
    f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso,
    f.dozvoljeno_komentarisanje, f.dozvoljeno_ocenjivanje,
    f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata,
    f.autorovkomentar, f.broj_favorita,
    f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava,
    a.id as ime from fotografija f, autori a
    where a.username = f.username) as pretraga  where $kljucne or $opis or $naziv order by $sortiranje desc $limit";
    $result = mysql_query($query) or die(mysql_error());
    $searchPhotos = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($searchPhotos, $row);
    }

    return $searchPhotos;
}

function getTotalSearchResult($kljucne, $opis, $naziv) {
    //global $link;
	$query = "select count(*) from (select f.id, f.username, f.naziv, f.opis, f.kljucne_reci, f.mesto_fotografisanja,
f.vreme_fotografisanja,
f.aparat, f.objektiv, f.filteri, f.tehnicki_detalji, f.ekspozicija, f.blenda, f.iso,
f.dozvoljeno_komentarisanje, f.dozvoljeno_ocenjivanje,
f.datum_postavljanja, f.slika, f.broj_skljoceva, f.broj_pregleda, f.galerija, f.modelaparata,
f.autorovkomentar, f.broj_favorita,
f.broj_komentara, f.ver1, f.ver2, f.opisver1, f.opisver2, f.boja, f.objava,
a.id as ime from fotografija f, autori a
where a.username = f.username) as pretraga  where $kljucne or $opis or $naziv";
    $result = mysql_query($query) or die(mysql_error());
    $totalSearchResult = mysql_fetch_array($result);

    return $totalSearchResult[0];
}

function totalSearchClanovi($user, $ime) {
    //global $link;
	$query = "select count(*) from pregled_fotografa where $user or $ime ";
    $result = mysql_query($query) or die(mysql_error());
    $totalSearchClanovi = mysql_fetch_array($result);

    return $totalSearchClanovi[0];
}

function getSearchClanoviList($user, $ime, $sortiranje, $limit) {
    //global $link;
	$query = "select * from pregled_fotografa where $user or $ime order by $sortiranje desc $limit ";
    $result = mysql_query($query) or die(mysql_error());
    $searchClanovi = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($searchClanovi, $row);
    }

    return $searchClanovi;
}

function deleteSmfMember($member_id) {
    $query = "delete from smf_members where id_member='$member_id';";
    $result = mysql_query($query) or die(mysql_error());
}

function getSmfMemberId($username) {
    //global $link;

    $query = "select id_member from smf_members where member_name='$username';";
    $result = mysql_query($query) or die(mysql_error());
    $memberid = mysql_fetch_array($result);

    return $memberid[0];
}

function totalSearchVideo($descript, $naslov, $tag) {
    $query = "select count(*) from video where $naslov or $descript or $tag";
    $result = mysql_query($query) or die(mysql_error());
    $totalSearchVideo = mysql_fetch_array($result);

    return $totalSearchVideo[0];
}

function searchVideo($naslov, $descript, $tag, $sortiranje, $limit) {
    $query = "select * from video where $naslov or $descript or $tag order by $sortiranje $limit;";
    $result = mysql_query($query) or die(mysql_error());
    $searchVideo = array();

    while ($row = mysql_fetch_array($result)) {
        array_push($searchVideo, $row);
    }

    return $searchVideo;
}
?>